import fs from 'fs'
import { sep, join } from 'path'
import os from 'os'

export default async (value: string, file?: string) => {
  let filename = ''
  if (file) {
    filename = file
  } else {
    const tmpDir = os.tmpdir();
    const name = await fs.promises.mkdtemp(`${tmpDir}${sep}`)
    filename = join(name, 'tmp.py')
  }
  await fs.promises.writeFile(filename, value)
  return filename
}